import store from "..";
import request from "../../common/utils/request";
import utils from "../../common/utils/utils";
const datav = {
  state: {
    // fd
    currentFD: 0,
    // 项目列表
    villageList: [],
    // 当前选中项目
    currentVillage: {},
    // 当前选中楼宇
    currentBuilding: {},
    // 当前选中菜单
    currentMenu: "",
    // websocket数据
    weather: {},
    synthetical: {},
    flow: {},
    rentControl: {},
    centerControl: {},
    security: {},
    syntheticalCustom: null, //自定义综合态势组件
    isSyntheticalCustom: null, //综合态势是否自定义
    // 提醒重置数据
    clearTrigger: 1,
    // 预览模式
    preview: true,
    previewAlert: false,
    // hiddenPreviewBtn: false,
    // 是否是移动端访问
    isMobile: false,
  },
  mutations: {
    SET_CURRENT_FD: (state, fd) => {
      state.currentFD = fd;
    },
    SET_VILLAGE_LIST: (state, list) => {
      state.villageList = list;
    },
    SET_CURRENT_VILLAGE: (state, village) => {
      if (village) {
        state.currentVillage = village || {};
      } else {
        state.currentVillage = {};
        state.currentBuilding = {};
      }
    },
    SET_CURRENT_BUILDING: (state, building) => {
      state.currentBuilding = building || {};
    },
    SET_DATAV_CURRENT_MENU: (state, menu) => {
      state.currentMenu = menu;
    },
    SET_MESSAGE_INFO: (state, { key1, key2, data }) => {
      if (!state[key1]) {
        state[key1] = {};
      }
      if (key2) {
        state[key1][key2] = data;
      } else {
        state[key1] = data;
      }

      if (key1 == "syntheticalCustom") {
        if (data.left && data.left.length > 0) {
          data.left.forEach((item) => {
            const type = item.data.type;
            const moduleData = item.data.data;
            let typeArr = type.split("-");
            if (typeArr.length > 2) {
              const typeArr0 = typeArr[0];
              const typeArr1 = typeArr.slice(1, typeArr.length);
              typeArr = [typeArr0, typeArr1.join("_")];
            }
            if (typeArr.length == 2) {
              let k1 = getRealKey(typeArr[0]) || typeArr[0];
              let k2 = typeArr[1];
              state[k1][k2] = moduleData;
            } else {
              let k1 = getRealKey(typeArr[0]) || typeArr[0];
              state[k1] = moduleData;
            }
          });
        }
        if (data.right && data.right.length > 0) {
          data.right.forEach((item) => {
            const type = item.data.type;
            const moduleData = item.data.data;
            let typeArr = type.split("-");
            if (typeArr.length > 2) {
              const typeArr0 = typeArr[0];
              const typeArr1 = typeArr.slice(1, typeArr.length);
              typeArr = [typeArr0, typeArr1.join("_")];
            }
            if (typeArr.length == 2) {
              let k1 = getRealKey(typeArr[0]) || typeArr[0];
              let k2 = typeArr[1];
              state[k1][k2] = moduleData;
            } else {
              let k1 = getRealKey(typeArr[0]) || typeArr[0];
              state[k1] = moduleData;
            }
          });
        }
      }
    },
    RESET_DATAV_DATA: (state) => {
      state.weather = {};
      state.synthetical = {};
      state.flow = {};
      state.rentControl = {};
      state.centerControl = {};
      state.security = {};
      state.hiddenPreviewBtn = false;
      state.syntheticalCustom = null;
      state.isSyntheticalCustom = null;
    },
    SET_CLEAR_TRIGGER: (state) => {
      state.clearTrigger++;
    },
    SET_PREVIEW_MODE: (state, preview) => {
      state.preview = preview;
    },
    SET_PREVIEW_MODE_ALERT: (state, alerted) => {
      state.previewAlert = alerted;
    },
    // SET_PREVIEW_BTN_HIDDEN: (state, hidden) => {
    //   state.hiddenPreviewBtn = hidden;
    // },
    SET_IS_MOBILE: (state) => {
      state.isMobile = utils.isMobile() || store.getters.isMobile;
    },
    SET_SYNTHETICAL_IS_CUSTOM: (state, data) => {
      state.isSyntheticalCustom = data;
    },
  },
  actions: {
    SetCurrentFD: ({ commit }, fd) => {
      commit("SET_CURRENT_FD", fd);
    },
    SetCurrentVillage: ({ commit }, village) => {
      return new Promise((resolve) => {
        commit("SET_CURRENT_VILLAGE", village);
        resolve();
      });
    },
    SetCurrentBuilding: ({ commit }, building) => {
      return new Promise((resolve) => {
        commit("SET_CURRENT_BUILDING", building);
        resolve();
      });
    },
    GetVillageList: ({ commit, state }) => {
      return new Promise((resolve) => {
        request
          .get("/org", "/build/villageAndBuildList", { checked: 1 })
          .then((res) => {
            commit("SET_VILLAGE_LIST", res.villageList);
            resolve(res.villageList);
          });
      });
    },
    GetCurrentVillageAndBuild: ({ state, commit, dispatch }) => {
      return new Promise((resolve) => {
        const { currentVillage, currentBuilding } = state;
        dispatch("GetVillageList").then((list) => {
          let result = {
            villageList: list,
            currentVillage: {},
            currentBuilding: {},
          };
          if (currentVillage.id) {
            const index = list.findIndex(
              (item) => item.id == currentVillage.id
            );
            if (index > -1) {
              result.currentVillage = list[index];
              if (currentBuilding.id) {
                const index = currentVillage.buildList.findIndex(
                  (item) => item.id == currentBuilding.id
                );
                if (index > -1) {
                  result.currentBuilding = currentBuilding;
                } else {
                  commit("SET_CURRENT_BUILDING");
                }
              }
            } else {
              commit("SET_CURRENT_VILLAGE");
              commit("SET_CURRENT_BUILDING");
            }
          }
          resolve(result);
        });
      });
    },
    SetDataVCurrentMenu: ({ commit }, menu) => {
      return new Promise((resolve) => {
        if (menu.indexOf("-") == -1 && menuMapTabs[menu]) {
          menu = `${menu}-${menuMapTabs[menu][0]}`;
        }
        commit("SET_DATAV_CURRENT_MENU", menu);
        resolve();
      });
    },
    SetDataVMessage: ({ commit }, msg) => {
      const { type, data } = msg;
      if (type == "Synthetical-ModuleTotal") {
        commit("SET_MESSAGE_INFO", {
          key1: "syntheticalCustom",
          data: data,
        });
        commit("SET_SYNTHETICAL_IS_CUSTOM", true);
      } else {
        let typeArr = type.split("-");
        if (typeArr.length > 2) {
          const typeArr0 = typeArr[0];
          const typeArr1 = typeArr.slice(1, typeArr.length);
          typeArr = [typeArr0, typeArr1.join("_")];
        }
        if (typeArr.length == 2) {
          commit("SET_MESSAGE_INFO", {
            key1: getRealKey(typeArr[0]) || typeArr[0],
            key2: typeArr[1],
            data: data,
          });
        } else {
          commit("SET_MESSAGE_INFO", {
            key1: getRealKey(typeArr[0]) || typeArr[0],
            data: data,
          });
        }
        if (getRealKey(typeArr[0]) == "synthetical") {
          commit("SET_SYNTHETICAL_IS_CUSTOM", false);
        }
      }
    },
    ResetDataVInfo: ({ commit }) => {
      return new Promise((resolve) => {
        commit("RESET_DATAV_DATA");
        resolve();
      });
    },
    SetClear: ({ commit }) => {
      commit("SET_CLEAR_TRIGGER");
    },
    SetPreviewMode: ({ commit }, preview) => {
      return new Promise((resolve) => {
        commit("SET_PREVIEW_MODE", preview);
        resolve();
      });
    },
    SetPreviewModeAlert: ({ commit }, alertStatus) => {
      commit("SET_PREVIEW_MODE_ALERT", alertStatus);
    },
    // SetPreviewModeHidden: ({ commit }, hidden) => {
    //   commit("SET_PREVIEW_BTN_HIDDEN", hidden);
    // },
    SetIsMobile: ({ commit }) => {
      commit("SET_IS_MOBILE");
    },
    SetSyntheticalIsCustom: ({ commit }, data) => {
      return new Promise((resolve) => {
        commit("SET_SYNTHETICAL_IS_CUSTOM", data);
        resolve();
      });
    },
  },
};

const getRealKey = (str) => {
  return MENU_NAME_MAPS[str];
};

const MENU_NAME_MAPS = {
  Synthetical: "synthetical",
  Flow: "flow",
  LeaseManage: "rentControl",
  CenterControl: "centerControl",
  Security: "security",
};
const menuMapTabs = {
  centerControl: ["energy", "device", "park", "meetingRoom"],
};

export default datav;
